// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
const uniID = require('uni-id-common')

let dbJQL = null
let publishCollection = null
module.exports = {
	async _before() {
		const clientInfo = this.getClientInfo()
		dbJQL = uniCloud.databaseForJQL({
			clientInfo
		})
		publishCollection = dbJQL.collection('publish-record')

		this.uniIDIns = uniID.createInstance({
			clientInfo
		})

		const methodName = this.getMethodName()
		if (methodName === 'search') {
			return
		}

		const token = this.getUniIdToken()
		if (!token) {
			throw new Error('token不存在')
		}
	},
	async add(params = {}) {
		const {
			data
		} = await dbJQL.collection('uni-id-users')
			.where({
				_id: dbJQL.getCloudEnv('$cloudEnv_uid')
			})
			.field("nickname,avatar")
			.get()
		const userInfo = data[0]
		const {
			address,
			...rest
		} = params
		const res = await publishCollection
			.add({
				publisher: {
					id: userInfo._id,
					nickname: userInfo.nickname || '',
					avatar: userInfo.avatar || ''
				},
				status: 1,
				address: new dbJQL.Geo.Point(...address),
				...rest
			})
		return {
			errCode: 0,
			data: {
				id: res.id
			},
			errMsg: '新增成功'
		}
	},
	async update(params) {
		const {
			id,
			...rest
		} = params
		try {
			await publishCollection
				.where({
					_id: id
				})
				.update(rest)
			return {
				errCode: 0,
				data: null,
				errMsg: '更新成功'
			}
		} catch (e) {
			throw new Error(e.message)
		}
	},
	async delete(params) {
		const {
			id,
			...rest
		} = params
		try {
			await publishCollection
				.where({
					_id: id
				})
				.update({
					status: 0
				})
			return {
				errCode: 0,
				data: null,
				errMsg: '删除成功'
			}
		} catch (e) {
			throw new Error(e.message)
		}
	},
	async searchByName(params = {}) {
		const {
			pageNo = 1, pageSize = 10
		} = params

		try {
			if (pageSize > 20) {
				throw new Error('单页数据不可超过100条')
			}
			const skip = (pageNo - 1) * pageSize
			const res = await publishCollection
				.where(`${new RegExp(params.name, 'i')}.test(content) && status == 1`)
				.skip(skip)
				.limit(pageSize)
				.get()
			return {
				errCode: 0,
				data: res.data,
				errMsg: '查询成功'
			}
		} catch (e) {
			throw new Error(e.message)
		}
	},
	async search(params) {
		try {
			const {
				pageNo = 1, pageSize = 10
			} = params
			if (pageSize > 20) {
				throw new Error('单页数据不可超过100条')
			}
			const skip = (pageNo - 1) * pageSize

			const query = {
				status: 1
			}
			if (params.tag !== undefined) {
				query.tag = params.tag
			}
			if (params.type !== undefined) {
				query.type = params.type
			}

			let orderBy = 'createTime desc'
			if (params.sort === 1) {
				orderBy = 'updateTime desc'
			} else if (params.sort === 2) {
				orderBy = 'distance asc'
			} else if (params.sort === 3) {
				orderBy = 'price asc'
			}

			const res = await publishCollection
				.geoNear({
					distanceField: 'distance',
					spherical: true,
					near: new dbJQL.Geo.Point(...params.address),
					minDistance: 0,
					maxDistance: params.distance || 2000,
					query: query
				})
				// .field("publisher,content,imgUrls,updateTime,createTime,price")
				.orderBy(orderBy)
				.skip(skip)
				.limit(pageSize)
				.get()
			return {
				errCode: 0,
				data: res.data,
				errMsg: '查询成功'
			}
		} catch (e) {
			throw new Error(e.message)
		}
	},
	async getPublishById(params = {}) {
		try {
			if (!params.id) {
				throw new Error('缺少查询参数id')
			}
			const res = await publishCollection
				.where({
					_id: params.id,
					status: 1
				})
				.get()
			return {
				errCode: 0,
				data: res.data[0],
				errMsg: '删除成功'
			}
		} catch (e) {
			throw new Error(e.message)
		}
	},
	async getMyPublish(params = {}) {
		const {
			pageNo = 1, pageSize = 10
		} = params

		try {
			if (pageSize > 20) {
				throw new Error('单页数据不可超过100条')
			}
			const skip = (pageNo - 1) * pageSize
			const res = await publishCollection
				.where('publisher.id==$cloudEnv_uid && status != 0')
				.orderBy('createTime asc')
				.skip(skip)
				.limit(pageSize)
				.get()
			return {
				errCode: 0,
				data: res.data,
				errMsg: '查询成功'
			}
		} catch (e) {
			throw new Error(e.message)
		}
	}
}